package com.luzhikun.ajax.filter;

import javax.servlet.*;
import javax.servlet.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/*
 * @Author liu-miss
 * @Description 改变字符集
 * @Date 13:21 2021/5/9
 **/
@WebFilter(filterName = "CharsetFilter",value = "/*")
public class CharsetFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
        // 获取请求对象
        HttpServletRequest req=(HttpServletRequest) request;
        // 获取响应对象
        HttpServletResponse resp=(HttpServletResponse) response;
        // 改变响应和请求的字符集
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        // 放行
        chain.doFilter(request,response);

    }

    @Override
    public void destroy() {

    }
}
